function [act_mat, max_act]=act_summary(ROIs,pl)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   summary of population activity displayed as traces, imagesc and
%   overlayed traces
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if iscell(ROIs)
    if nargin>1
        cnt=pl;
    end
    try
        for rnd=cnt:cnt
            act_mat=[];
            max_act_mat=[];
            for gnd=1:length(ROIs{cnt})
                act=ftfil(ROIs{cnt}(gnd).activity,18,0,10);
                act=psmooth(act);
                raw_act=act/median(act);
                max_act=max(raw_act);
                act_mat(gnd,:)=raw_act;
                max_act_mat(gnd)=max_act;
            end
            max_act=max(max_act_mat);
            figure;
            subplot(3,1,1)
            hold on
            title(['layer: ' num2str(cnt) ', n = ' num2str(size(act_mat,1))])
            xlim([0 size(act_mat,2)])
            ylim([0 size(act_mat,1)])
            set(gca,'position',[0.02 0.65 0.96 0.3])
            for knd=1:length(ROIs{cnt})
                plot((act_mat(knd,:)/max_act)+(knd-1),'color','black')
            end
            
            subplot(3,1,2)
            hold on
            xlim([0 size(act_mat,2)])
            ylim([0 size(act_mat,1)+1])
            set(gca,'position',[0.02 0.33 0.96 0.3])
            imagesc(act_mat)
            
            subplot(3,1,3)
            hold on
            xlim([0 size(act_mat,2)])
            ylim([1.1 max_act])
            set(gca,'position',[0.02 0.01 0.96 0.3])
            plot(act_mat')
            drawnow
        end
    catch
        for rnd=1:length(ROIs)
            act_mat=[];
            max_act_mat=[];
            for gnd=1:length(ROIs{rnd})
                act=ftfil(ROIs{rnd}(gnd).activity,18,0,10);
                act=psmooth(act);
                raw_act=act/median(act);
                max_act=max(raw_act);
                act_mat(gnd,:)=raw_act;
                max_act_mat(gnd)=max_act;
            end
            max_act=max(max_act_mat);
            figure;
            subplot(3,1,1)
            hold on
            title(['layer: ' num2str(rnd) ', n = ' num2str(size(act_mat,1))])
            xlim([0 size(act_mat,2)])
            ylim([0 size(act_mat,1)])
            set(gca,'position',[0.02 0.65 0.96 0.3])
            for knd=1:length(ROIs{rnd})
                plot((act_mat(knd,:)/max_act)+(knd-1),'color','black')
            end
            subplot(3,1,2)
            hold on
            xlim([0 size(act_mat,2)])
            ylim([0 size(act_mat,1)+1])
            set(gca,'position',[0.02 0.33 0.96 0.3])
            imagesc(act_mat)
            
            subplot(3,1,3)
            hold on
            xlim([0 size(act_mat,2)])
            ylim([1.1 max_act])
            set(gca,'position',[0.02 0.01 0.96 0.3])
            plot(act_mat')
            drawnow
        end
    end
else
    act_mat=[];
    max_act_mat=[];
    for gnd=1:length(ROIs)
        act=ftfil(ROIs(gnd).activity,18,0,10);
        act=psmooth(act);
        raw_act=act/median(act);
        max_act=max(raw_act);
        act_mat(gnd,:)=raw_act;
        max_act_mat(gnd)=max_act;
    end
    max_act=max(max_act_mat);
    figure;
    subplot(3,1,1)
    hold on
    title(['n = ' num2str(size(act_mat,1))])
    xlim([0 size(act_mat,2)])
    ylim([0 size(act_mat,1)])
    set(gca,'position',[0.02 0.65 0.96 0.3])
    for knd=1:length(ROIs)
        plot((act_mat(knd,:)/max_act)+(knd-1),'color','black')
    end
    
    subplot(3,1,2)
    hold on
    xlim([0 size(act_mat,2)])
    ylim([0 size(act_mat,1)+1])
    set(gca,'position',[0.02 0.33 0.96 0.3])
    imagesc(act_mat)
    
    subplot(3,1,3)
    hold on
    xlim([0 size(act_mat,2)])
    ylim([1.1 max_act])
    set(gca,'position',[0.02 0.01 0.96 0.3])
    plot(act_mat')
    drawnow
end